package com.ruoyi.mall.app.service.impl;



import com.ruoyi.mall.app.repository.DtsCategoryEsRepository;
import com.ruoyi.mall.app.repository.DtsGoodsEsRepository;
import com.ruoyi.mall.app.service.DtsGoodsService;
import com.ruoyi.mall.app.service.DtsKeywordService;
import com.ruoyi.mall.common.domain.*;
import com.ruoyi.mall.common.mapper.DtsGoodsApiMapper;
import com.ruoyi.mall.common.mapper.DtsGoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DtsGoodsServiceImpl implements DtsGoodsService {

    @Autowired
    private DtsGoodsApiMapper dtsGoodsApiMapper;

    @Autowired
    private DtsGoodsMapper dtsGoodsMapper;

    @Autowired
    private DtsKeywordService keywordService;

    @Autowired
    private DtsCategoryEsRepository categoryEsRepository;

    @Autowired
    private DtsGoodsEsRepository goodsEsRepository;

    @Autowired
    public RedisTemplate  redisTemplate;

    @PostConstruct
    public void init()
    {
        DtsGoods dtsGoods = new DtsGoods();
        dtsGoods.setIsNew(1);
        List<DtsGoods>  list = dtsGoodsMapper.selectDtsGoodsList1(dtsGoods);

        DtsGoods dtsGoods1 = new DtsGoods();
        dtsGoods1.setIsHot(1);
        List<DtsGoods>  list1 = dtsGoodsMapper.selectDtsGoodsList(dtsGoods1);


        redisTemplate.opsForValue().set("goodsNewList", list);
        redisTemplate.opsForValue().set("goodsHotList", list1);
    }


    @Override
    public List<DtsGoods> selectGoodsNew(DtsGoods dtsGoods) {
        dtsGoods.setIsNew(1);

        // 判断
        if (dtsGoods.getCounterPrice() != null || dtsGoods.getRetailPrice() != null) {
            List<DtsGoods>  tmp1 = dtsGoodsMapper.selectDtsGoodsList1(dtsGoods);
            redisTemplate.opsForValue().set("goodsNewList", tmp1);
        }

        List<DtsGoods> list = (List<DtsGoods>) redisTemplate.opsForValue().get("goodsNewList");
        if (list != null) {
            return list;
        }
        List<DtsGoods>  tmp = dtsGoodsMapper.selectDtsGoodsList1(dtsGoods);
        if (tmp != null) {
            redisTemplate.opsForValue().set("goodsNewList", tmp);
        }
        return dtsGoodsMapper.selectDtsGoodsList(dtsGoods);
    }

    @Override
    public List<DtsGoods> selectGoodsHot(DtsGoods dtsGoods) {
        dtsGoods.setIsHot(1);
        // 判断
        if (dtsGoods.getCounterPrice() != null || dtsGoods.getRetailPrice() != null) {
            List<DtsGoods>  tmp1 = dtsGoodsMapper.selectDtsGoodsList(dtsGoods);
            redisTemplate.opsForValue().set("goodsHotList", tmp1);
        }

        List<DtsGoods> list = (List<DtsGoods>) redisTemplate.opsForValue().get("goodsHotList");
        if (list != null) {
            return list;
        }
        List<DtsGoods>  tmp = dtsGoodsMapper.selectDtsGoodsList(dtsGoods);
        if (tmp != null) {
            redisTemplate.opsForValue().set("goodsHotList", tmp);
        }
        return dtsGoodsMapper.selectDtsGoodsList(dtsGoods);
    }

    @Override
    public DtsGoods selectGoodsById(long id) {
        dtsGoodsApiMapper.updateGoodsById(id);
        return dtsGoodsApiMapper.selectGoodsById(id);
    }

    @Override
    public List<DtsGoods> selectGoodsByCateId(Long categoryId) {
        DtsGoods tmp = new DtsGoods();
        tmp.setCategoryId(categoryId);
        return dtsGoodsMapper.selectDtsGoodsList(tmp);
    }

    @Override
    public List<DtsGoods> selectGoodsByCategoryId(long categoryId) {
        DtsGoods tmp = new DtsGoods();
        tmp.setCategoryId(categoryId);
        return dtsGoodsMapper.selectDtsGoodsList(tmp);
    }

    @Override
    public List<DtsGoods> selectGoodsByDtsGoods(DtsGoods dtsGoods, long orderPrice) {
        // 查询是否有关键字
        int isKN = keywordService.selectKeywordByKeyword(dtsGoods.getName());
        // 有关键字就关键字搜索
        if (isKN >= 1) {
            dtsGoods.setKeywords(dtsGoods.getName());
        } else {  // 没有就名称搜索
            dtsGoods.setName(dtsGoods.getName());
        }
        return dtsGoodsApiMapper.selectGoodsByDtsGoods(dtsGoods, orderPrice);
    }

    @Override
    public DtsCategoryVo selectGoodsByCategoryVo(DtsCategory dtsCategory) {
        DtsCategoryVo categoryVo = new DtsCategoryVo();
        categoryVo.setId(dtsCategory.getId());
        categoryVo.setName(dtsCategory.getName());
//        categoryVo.setPid(dtsCategory.getPid());
        categoryVo.setGoodsList(dtsGoodsApiMapper.selectGoodsByCategoryVo(dtsCategory));
        return categoryVo;
    }

    @Override
    public List<DtsGoodsAttribute> selectGoodsAttribute(Long id) {
        return dtsGoodsApiMapper.selectGoodsAttribute(id);
    }

    @Override
    public List<DtsGoodsEs> searchGoodsEs(String name, Integer orderBy) {
        // 判断name是否有值
        if (name == null || name.equals("")) {
            return goodsEsRepository.findAll();
        }
        return goodsEsRepository.findByNameContaining(name);
    }

    @Override
    public List<DtsCategoryEs> selectCategoryEs(String name) {
        // 判断name是否有值
        if (name == null || name.equals("")) {
            return categoryEsRepository.findAll();
        }
        List<DtsGoodsEs> list = goodsEsRepository.findByNameContaining(name);
        List<String> ids = new ArrayList<>();
        for (DtsGoodsEs goodsEs : list) {
            ids.add(goodsEs.getCategoryId().toString());
        }
        // ids去重
        ids = ids.stream().distinct().collect(Collectors.toList());
        // list 转换为 Iterable
        Iterable<String> iterable = ids;
        return categoryEsRepository.findByIdIn(iterable);
    }

    @Override
    public List<DtsCategory> selectCategory(DtsCategory dtsCategory) {
        return null;
    }


}
